

#include <gtk/gtk.h>
#include <stdio.h>
#include "support.h"
#include "config.h"
#include "memview.h"
#include "gsim.h"

/*################## globals #########################*/
struct color_config gsim_colors;
struct font_config gsim_fonts;
struct file_config gsim_files;
struct value_config gsim_values;

/*#################### Static functions ####################*/
static void init_config_defaults(void);

/*################## Initilise the all structures with defaults ################*/
void init_config_defaults(void)
{
	g_strlcpy(gsim_colors.codeview_addr_fg,"",30);
	g_strlcpy(gsim_colors.codeview_addr_bg,"NavyBlue",30);
	g_strlcpy(gsim_colors.codeview_label_fg,"purple4",30);
	g_strlcpy(gsim_colors.codeview_label_bg,"white",30);
	g_strlcpy(gsim_colors.codeview_mne_fg,"NavyBlue",30);
	g_strlcpy(gsim_colors.codeview_mne_bg,"WhiteSmoke",30);
	g_strlcpy(gsim_colors.codeview_op1_fg,"blue",30);
	g_strlcpy(gsim_colors.codeview_op1_bg,"snow",30);
	g_strlcpy(gsim_colors.codeview_op2_fg,"RoyalBlue4",30);
	g_strlcpy(gsim_colors.codeview_op2_bg,"",30);
	g_strlcpy(gsim_colors.codeview_comment_fg,"",30);
	g_strlcpy(gsim_colors.codeview_comment_bg,"",30);
	
	g_strlcpy(gsim_colors.memview_addr_fg,"",30);
	g_strlcpy(gsim_colors.memview_addr_bg,"NavyBlue",30);
	g_strlcpy(gsim_colors.memview_data_fg,"blue",30);
	g_strlcpy(gsim_colors.memview_data_bg,"AliceBlue",30);
	
	g_strlcpy(gsim_colors.ioview_addr_fg,"",30);
	g_strlcpy(gsim_colors.ioview_addr_bg,"NavyBlue",30);
	g_strlcpy(gsim_colors.ioview_data_fg,"blue",30);
	g_strlcpy(gsim_colors.ioview_data_bg,"AliceBlue",30);
	
	g_strlcpy(gsim_fonts.codeview_addr_font,"",50);
	g_strlcpy(gsim_fonts.codeview_label_font,"",50);
	g_strlcpy(gsim_fonts.codeview_mne_font,"",50);
	g_strlcpy(gsim_fonts.codeview_op1_font,"",50);
	g_strlcpy(gsim_fonts.codeview_op2_font,"",50);
	g_strlcpy(gsim_fonts.codeview_comment_font,"",50);
	g_strlcpy(gsim_fonts.memview_addr_font,"",50);
	g_strlcpy(gsim_fonts.memview_data_font,"",50);
	g_strlcpy(gsim_fonts.ioview_addr_font,"",50);
	g_strlcpy(gsim_fonts.ioview_data_font,"",50);

    gchar *tch;
#ifdef G_OS_WIN32
    tch = g_build_filename (package_data_dir, "gsim2.rc", NULL);
#else	
    tch = g_build_filename (PACKAGE_DATA_DIR, "gsim2.rc", NULL);
//	g_strlcpy(gsim_files.style,PACKAGE_DATA_DIR"/gsim2.rc",256);
#endif
    g_strlcpy(gsim_files.style,tch,256);
    g_free(tch);
    
	int i;
	for(i=0;i<10;i++)
	{
		g_strlcpy(gsim_files.recent[i],"",50);
		g_strlcpy(gsim_files.opened[i],"",50);
	}
	
	g_strlcpy(gsim_values.memfind,"",5);
	g_strlcpy(gsim_values.iofind,"",3);
	g_strlcpy(gsim_values.start_address,"",5);
	
	for(i=0;i<13;i++)
		g_strlcpy(gsim_values.flags[i],"0",2);
	for(i=0;i<8;i++)
		g_strlcpy(gsim_values.reg_2[i],"00",3);
	for(i=0;i<2;i++)
		g_strlcpy(gsim_values.reg_4[i],"0000",5);
	for(i=0;i<256;i++)
		gsim_values.port_data[i]=0;
	gsim_values.memstart=0x0000;
	gsim_values.memend=0xffff;
	
	return;
}

/*####################### Make a new config file ###########################*/
gint make_config_file(void)
{
	FILE *config_file;
	const gchar *home_dir;
	gchar *conf_file;
	home_dir=g_get_home_dir();
	conf_file=g_build_filename(home_dir,".gsim_config2",NULL);
	config_file=fopen(conf_file,"wb");
	g_free(conf_file);
	if(config_file==NULL)
		return 0;
	fwrite(&gsim_colors,1,sizeof(struct color_config),config_file);
	fwrite(&gsim_fonts,1,sizeof(struct font_config),config_file);
	fwrite(&gsim_files,1,sizeof(struct file_config),config_file);
	fwrite(&gsim_values,1,sizeof(struct value_config),config_file);
	
	fclose(config_file);
	
	return 1;
}

/*################## Load a config file ##########################*/
gint load_config_file(void)
{
	FILE *config_file;
	const gchar *home_dir;
	gchar *conf_file;
	home_dir=g_get_home_dir();
	conf_file=g_build_filename(home_dir,".gsim_config2",NULL);
	config_file=fopen(conf_file,"rb");
	g_free(conf_file);
	if(config_file==NULL)
	{
		init_config_defaults();
		return 0;
	}
	fread(&gsim_colors,1,sizeof(struct color_config),config_file);
	fread(&gsim_fonts,1,sizeof(struct font_config),config_file);
	fread(&gsim_files,1,sizeof(struct file_config),config_file);
	fread(&gsim_values,1,sizeof(struct value_config),config_file);
	fclose(config_file);
	return 1;
}
/*
int main(void)
{
	init_config_defaults();
	make_new_config_file();
	
	return 0;
}*/
